<template>
	<view>
		<view class="zhanwei"></view>
		<view class="item flex">
			<view class="item_left">报销事项</view>
			<input type="text" placeholder="输入报销事项" v-model="from.event" class="item_right" />
		</view>
		<view class="item flex">
			<view class="item_left">报销金额</view>
			<input type="digit" placeholder="输入金额" v-model="from.amount" class="item_right" />
		</view>

		<view class="item flex">
			<view class="item_left">报销人</view>
			<input type="text" placeholder="输入报销人" v-model="from.name" class="item_right" />
		</view>
		<view class="item flex">
			<view class="item_left">报销账户</view>
			<input type="text" placeholder="输入报销账户" v-model="from.account" class="item_right" />
		</view>
		<view class="item flex">
			<view class="item_left">开户行</view>
			<input type="text" placeholder="输入开户行" v-model="from.bank_name" class="item_right" />
		</view>
		<view class="item flex" @click="choose_file">
			<view class="item_left">上传发票</view>
			<input type="text" disabled placeholder="选择pdf文件" style="width: 55%" class="item_right" v-if="!from.fileName"/>
			<view class="item_right line-1" style="width: 55%" v-else>
				{{from.fileName}}
			</view>
			<u-icon name="arrow-right" color="#333" size="13"></u-icon>
		</view>
		<view class="bottom">
			<view class="bottom_btn" @click="determine">提交申请</view>
		</view>
	</view>
</template>

<script>
import { expenseinfo, expensehandle } from '@/api/common.js';
import uploadImage from '@/common/ossutil/uploadFile.js';
export default {
	data() {
		return {
			show: false,
			value1: '',
			from: {
				event:'',
				amount:'',
				name: '',
				account: '',
				bank_name: '',
				file: '',
				fileName:''
			},
			issubmit:false,
		};
	},
	onLoad() {
		this.getinfo();
	},
	methods: {
		getinfo() {
			let t = this;
			expenseinfo().then((res) => {
				console.log(res);
				if (res.code == 200001) {
					let info = res.data;
					for (const key in t.from) {
						if (t.from.hasOwnProperty(key)) {
							if (info[key]) {
								t.from[key] = info[key];
							}
						}
					}
				}
			});
		},
		determine() {
			if(this.issubmit){
				return
			}else{
				this.issubmit = true;
			}
			let t = this;
			expensehandle(t.from).then((res) => {
				console.log(res);
				if(res.code == 200001){
					uni.showToast({
						title:'提交成功',
						icon:'none'
					})
					setTimeout(()=>{
						uni.navigateBack()
						t.issubmit = false;
					},800)
				}else{
					uni.showToast({
						title:res
					})
				}
			}).catch(res=>{
				t.issubmit = false;
			});
		},
		choose_time() {
			this.show = true;
		},
		confirm(e) {
			console.log(e);
		},
		choose_file() {
			let t = this
			uni.chooseMessageFile({
				count: 1,
				type: 'file',
				success(res) {
					console.log(res);
					let filepath = res.tempFiles[0].path;
					let lastindex = filepath.lastIndexOf('.');
					let type = filepath.substring(lastindex + 1, filepath.length);
					console.log(type);
					if(type != 'pdf'){
						uni.showToast({
							title:'您上传的文件不合法',
							icon:'none'
						})
						return false
					}
					uploadImage(filepath,res.tempFiles[0].name,'wechat/', (ress) => {
						console.log(ress)
						t.from.fileName = res.tempFiles[0].name
						t.from.file = ress.imgName
					});
				}
			});
		}
	}
};
</script>

<style>
.zhanwei {
	width: 100%;
	height: 30rpx;
}
.item {
	width: 100%;
	height: 104rpx;
	padding: 0 30rpx;
	box-sizing: border-box;
	border-bottom: 1rpx solid #f4f4f4;
}
.item_left {
	color: #333;
	font-family: Alibaba PuHuiTi 2;
	font-size: 32rpx;
	font-weight: 400;
	height: 104rpx;
	line-height: 104rpx;
}
.item_right {
	width: 70%;
	height: 104rpx;
	line-height: 104rpx;
	font-size: 32rpx;
	font-weight: 400;
	color: #333;
}
.bottom {
	width: 100%;
	height: 140rpx;
	background-color: #fff;
	position: fixed;
	bottom: 0;
	display: flex;
	align-items: center;
}
.bottom_btn {
	width: 690rpx;
	height: 90rpx;
	line-height: 90rpx;
	text-align: center;
	border-radius: 45rpx;
	background: #0165fb;
	margin: 0 auto;
	margin-top: -15rpx;
	color: #fff;
	text-align: center;
	font-family: Alibaba PuHuiTi 2;
	font-size: 30rpx;
	font-style: normal;
	font-weight: 400;
}
.time {
	height: 600rpx;
}
</style>
